package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.command.CChangeUsernameAndPasswordCommand;
import org.lalib.command.Command;
import org.lalib.command.FoodMatchCommand;
import org.lalib.command.ItemUpdateCommand;
import org.lalib.command.TableMatchCommand;
import org.lalib.command.ItemEditCommand;
import org.lalib.command.UserFoodMatchCommand;
import org.lalib.command.UserMatchCommand;
import org.lalib.command.UserTableMatchCommand;
import org.lalib.controller.form.CCommandForm;
import org.lalib.entity.object.EFood;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.ETable;
import org.lalib.entity.object.EUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cCommandAction")
public class CCommandAction implements IControllerAction {

	@Autowired
	CCommandForm cCommandForm;

	private static final long serialVersionUID = 1L;

	public String init(EItem item) {
		cCommandForm.setItem(item);
		cCommandForm.setFoodCommandList(getFoodCommandList(item));
		cCommandForm.setTableCommandList(getTableCommandList(item));
		cCommandForm.setUserCommandList(getUserCommandList(item));
		return "commands";
	}

	public String getBundleNameFromGivenClass(Object command) {
		return "commandName." + command.getClass().getSimpleName();
	}

	private List<Command> getUserCommandList(EItem item) {
		List<Command> userCommandList = new ArrayList<>();
		userCommandList.add(new UserMatchCommand(item));
		userCommandList.add(new UserTableMatchCommand(item));
		userCommandList.add(new UserFoodMatchCommand(item));
		userCommandList.add(new ItemEditCommand(item));
		userCommandList.add(new ItemUpdateCommand(item));
		userCommandList.add(new CChangeUsernameAndPasswordCommand(item));
		return userCommandList;
	}

	private List<Command> getFoodCommandList(EItem item) {
		List<Command> foodCommandList = new ArrayList<>();
		foodCommandList.add(new FoodMatchCommand(item));
		foodCommandList.add(new ItemEditCommand(item));
		foodCommandList.add(new ItemUpdateCommand(item));
		return foodCommandList;
	}

	private List<Command> getTableCommandList(EItem item) {
		List<Command> tableCommandList = new ArrayList<>();
		tableCommandList.add(new TableMatchCommand(item));
		tableCommandList.add(new ItemEditCommand(item));
		tableCommandList.add(new ItemUpdateCommand(item));
		return tableCommandList;
	}

	public boolean isItemUser() {
		return cCommandForm.getItem() instanceof EUser;
	}

	public boolean isItemFood() {
		return cCommandForm.getItem() instanceof EFood;
	}

	public boolean isItemTable() {
		return cCommandForm.getItem() instanceof ETable;
	}
}